<html lang="en">
	<head>
		<title>Yuka | Steering Behaviors | Pursuit</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			"Pursuit" is useful when an agent is required to intercept a moving agent.
		</p>
	</section>

	<script type="module">

		import * as YUKA from '../../../build/yuka.module.js';
		import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';
		import * as DAT from 'https://cdn.jsdelivr.net/npm/dat.gui@0.7.7/build/dat.gui.module.js';

		let renderer, scene, camera;

		let entityManager, time, pursuer, evader, target;

		init();
		animate();

		function init() {

			scene = new THREE.Scene();

			camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
			camera.position.set( 0, 20, 0 );
			camera.lookAt( scene.position );

			//

			const pursuerGeometry = new THREE.ConeBufferGeometry( 0.2, 1, 8 );
			pursuerGeometry.rotateX( Math.PI * 0.5 );
			const pursuerMaterial = new THREE.MeshNormalMaterial();

			const pursuerMesh = new THREE.Mesh( pursuerGeometry, pursuerMaterial );
			pursuerMesh.matrixAutoUpdate = false;
			scene.add( pursuerMesh );

			const evaderGeometry = new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 );
			const evaderMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );

			const evaderMesh = new THREE.Mesh( evaderGeometry, evaderMaterial );
			evaderMesh.matrixAutoUpdate = false;
			scene.add( evaderMesh );

			const grid = new THREE.GridHelper( 10, 25 );
			scene.add( grid );

			//

			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			//

			window.addEventListener( 'resize', onWindowResize, false );

			// game setup

			entityManager = new YUKA.EntityManager();
			time = new YUKA.Time();

			target = new YUKA.Vector3();

			evader = new YUKA.Vehicle();
			evader.maxSpeed = 3;
			evader.setRenderComponent( evaderMesh, sync );

			pursuer = new YUKA.Vehicle();
			pursuer.maxSpeed = 3;
			pursuer.position.z = - 5;
			pursuer.setRenderComponent( pursuerMesh, sync );

			const pursuitBehavior = new YUKA.PursuitBehavior( evader, 2 );
			pursuer.steering.add( pursuitBehavior );

			const seekBehavior = new YUKA.SeekBehavior( target );
			evader.steering.add( seekBehavior );

			entityManager.add( evader );
			entityManager.add( pursuer );

			// dat.gui

			const gui = new DAT.GUI( { width: 300 } );

			gui.add( pursuitBehavior, 'predictionFactor', 0, 5 ).name( 'prediction factor' );

			gui.open();

		}

		function onWindowResize() {

			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();

			renderer.setSize( window.innerWidth, window.innerHeight );

		}


		function animate() {

			requestAnimationFrame( animate );

			const deltaTime = time.update().getDelta();
			const elapsedTime = time.getElapsed();

			target.x = Math.cos( elapsedTime ) * Math.sin( elapsedTime * 0.2 ) * 6;
			target.z = Math.sin( elapsedTime * 0.8 ) * 6;

			entityManager.update( deltaTime );

			renderer.render( scene, camera );

		}

		function sync( entity, renderComponent ) {

			renderComponent.matrix.copy( entity.worldMatrix );

		}

	</script>

</body>
</html>
